﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace AxiosDemo.Controllers
{
    public class AxiosController : Controller
    {
        [HttpGet]
        public IActionResult Index(string name, string age)
        {
            //throw new Exception("测试Axios的catch!");

            List<Person> list = new List<Person>()
            {
                new Person(){ Id=1,Name="张三",Address="浙江金华" },
                 new Person(){ Id=2,Name="李四",Address="浙江金华" },
                  new Person(){ Id=3,Name="王五",Address="浙江金华" },
            };

            return Json(new { name = name, age = age, list = list });
        }

        [HttpPost]
        public IActionResult Index(string name, int age)
        {
            return Json(new { name = name, age = age });
        }


        ////application/json请求,[FromBody]接收的参数必须是一个模型类.如: public IActionResult Index([FromBody]string name,[FromBody]string age),将接收不到数据
        //[HttpPost]
        //public IActionResult Index([FromBody]People people)
        //{
        //    return Json(new { name = people.name, age = people.age});
        //}
    }

    public class People
    {
        public string name { get; set; }

        public int age { get; set; }
    }


    public class Person
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string Address { get; set; }
    }
}